

// nst_msg.c
#include "nst_msg.h"
#include <string.h>

void nst_msg_init(nst_msg_queue_t *queue, uint8_t *buffer, uint16_t size, uint16_t element_size) {
    queue->buffer = buffer;
    queue->head = 0;
    queue->tail = 0;
    queue->size = size;
    queue->element_size = element_size;
    memset(buffer, 0, size * element_size);
}

bool nst_msg_enqueue(nst_msg_queue_t *queue, const void *msg) {
    uint16_t next_head = (queue->head + 1) % queue->size;
    if (next_head == queue->tail) {
        return false; // 队列已满
    }
    memcpy(&queue->buffer[queue->head * queue->element_size], msg, queue->element_size);
    queue->head = next_head;
    return true;
}

bool nst_msg_dequeue(nst_msg_queue_t *queue, void *msg) {
    if (queue->head == queue->tail) {
        return false; // 队列为空
    }
    memcpy(msg, &queue->buffer[queue->tail * queue->element_size], queue->element_size);
    queue->tail = (queue->tail + 1) % queue->size;
    return true;
}

bool nst_msg_is_empty(const nst_msg_queue_t *queue) {
    return queue->head == queue->tail;
}

bool nst_msg_is_full(const nst_msg_queue_t *queue) {
    return ((queue->head + 1) % queue->size) == queue->tail;
}


